<?PHP
defined('_XR_EXEC') or die();
/**
 * 控制器处理类 test
 */
class Admin_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','系统管理');
		$this->assign('menuid1','1');  
	}
 

	
	
	
	##################################
	/* 管理首页 */
	################################## 
	function index__(){ 
	 	$this->assign('nav','管理首页');
	 	$this->assign('menuid1','1');
		$this->assign('menuid2','1'); 
		$db =  factory::getDBO();
	  //会员总数
		$sum_1 = $db->loadresult('select count(*) from t_user where usertype=2 and deleted=0  ');
		//申请会员总数
		$sum_2 = $db->loadresult('select count(*) from t_user where usertype=2 and deleted=0 and state=0  ');
		$this->assign('sum_1',$sum_1);
		$this->assign('sum_2',$sum_2); 
		
		

		
		
	
	} 
	
	
	##################################
	/* 修改密码 */
	##################################
	function pwd__(){
		//权限判断
		factory::module('admin')->authSys('pwd');
					
		$this->assign('nav','密码修改'); 
		$this->assign('menuid1','1');
		
		
		
		
		
		
		
		
		
		
		
	}
	function ajax_chgpwd__(){
		//权限判断
		factory::module('admin')->authSys('pwd');
		
	  $_POST = format::addslash_deep($_POST);
		$db = factory::getDBO();
		$m_admin = factory::module('admin');
		$sql = 'select * from `#__user` where id="'.$m_admin->getuserid().'" and password="'.func::madepwd($_POST['old_p']).'" ';
		if($db->loadassoc($sql)){
			$arr = array(
				"id" => $m_admin->getuserid(),
				"password" => func::madepwd($_POST['new_p'])
			);
			if($db->update('user',$arr)){
				exit('{success:1,msg:"修改密码成功!"}');
			}else{
				exit('系统繁忙，请稍后重试！');
			}
		}else{
			exit('修改密码失败，原密码输入有误！');
		}
	}	
	##################################
	/* 信息类别维护,特殊方法注意隐藏 */
	##################################
	function zp__(){
		if(!isset($_GET['date']) || $_GET['date']!=date('Y-m-d')){
			exit();
		}
		$this->assign('nav','信息类别维护'); 
		$this->assign('menuid1','1'); 
		
		$types = factory::module('pentype')->load_type(); 
 //	debug::show($types);
		$this->assign('types',$types); 
		
		
	}
	function pentype_add__(){
		$db = factory::getDBO();
		#得到传递过来的参数
		$_POST=format::addslash_deep($_POST);   
		$arrnames = explode(' ',$_POST['name']);
		//debug::show($arrnames); 
		foreach($arrnames as $names){
			if($names == ''){
				continue;
			}
			if($db->insert('pentype',array( 'name'=>$names,'parent_id'=>$_POST['parent_id'],'isleaf'=>'1' ))){
				# 设置父亲节点为不是叶子节点
				if($_POST['parent_id'] != '0'){
					$db->update('pentype',array(
						'id'=>$_POST['parent_id'],
						'isleaf'=>'0'
					));
				}
			}else{
				exit('系统繁忙，请稍后重试！');
			}	
		}
		exit('{success:1}');
	}
	function type_del__(){
		$_POST=format::addslash_deep($_POST); 
		$db = factory::getDBO();
		$id = intval($_POST['id']);
		$childs = $db->loadresult('select count(*) from `t_pentype` where parent_id="'.$id.'" ');
		if($childs > 0){
			exit('该类别下存在子类别，删除失败！');
		}
		$rs = $db->loadassoc('select * from `t_pentype` where id="'.$id.'" '); 
		if($db->query('delete from `t_pentype` where id="'.$id.'" ')){ 
			# 判断一下父节点，如果存在，并且父节点的子节点没有任何节点了，就应该设置为父亲节点
			if($rs['parent_id'] != '0'){
				if($db->loadresult('select count(*) from t_pentype where parent_id="'.$rs['parent_id'].'" ') == 0){
					$db->update('pentype',array(
						'id'=>$rs['parent_id'],
						'isleaf'=>'1'
					));
				}
			}
			exit('{success:1}');
		}else{
			exit('系统繁忙，请稍后重试！');
		} 
	}
	function type_modify__(){
		$objs = format::addslash_json(json_decode( stripslashes($_POST['objs']) ));
		$db = factory::getDBO(); 
		if($db->updatelist('pentype',$objs)){ 
			exit('{success:1,msg:"保存成功！"}');
		}else{
			exit('系统繁忙，请稍后重试！');
		}
	}	
	
	
	 
	
	
	##################################
	/* 权限组管理 */
	##################################
	function authgroup__(){
		//权限判断
		factory::module('admin')->authSys('authgroup');
		$this->assign('nav','权限组管理'); 
		$this->assign('menuid1','1'); 
		$this->assign('menuid2','3'); 
		
		
		
		 
	}
	function authgroup_list__(){
		//权限判断
		factory::module('admin')->authSys('authgroup');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';  
  	  #查询条件
			$sql_where = ' where 2=2 '.func::speedsearch($_POST['keywords'],array( 'a.name','a.remark' ));  
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_authgroup` a 
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select 
					a.*
				from `t_authgroup` a
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}  
	} 
	# 添加 
	public function authgroup_add__(){ 
		$this->assign('menuid2','3'); 
		//权限判断
		factory::module('admin')->authSys('authgroup');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$obj_sysmods = format::addslash_json(json_decode( stripslashes($_POST['obj_sysmods']) ));
			$obj_main = format::addslash_json(json_decode( stripslashes($_POST['obj_main']) ));
		 
			$db = factory::getDBO(); 
			$db->begin();
			//主表
			$id = $db->insert('authgroup',$obj_main);
			if(!$id){
				$db->rollback();
				exit('系统繁忙，请稍后重试！');
			}
			//明细表插入
			foreach($obj_sysmods as $v){ 
				$db->insert('authgroup_sysmod',array(
					'authgroup_id'=>$id,
					'sysmod_id'=>$v
				)); 
			}
			$db->commit();
			exit('{success:1,msg:"添加权限组成功！"}');
		} 
		$this->assign('nav','权限组管理 - 添加权限组');
		$db=factory::getDBO(); 
		$sysmods = $db->loadassoclist('select a.*  from `t_sysmod` a where 0=0 order by  a.show_index ');  
		$this->assign('sysmods',$sysmods); 
	}
	# 修改 
	public function authgroup_modify__(){ 
		$this->assign('menuid2','3'); 
		//权限判断
		factory::module('admin')->authSys('authgroup');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$obj_sysmods = format::addslash_json(json_decode( stripslashes($_POST['obj_sysmods']) )); 
			$obj_main = format::addslash_json(json_decode( stripslashes($_POST['obj_main']) ));
		 // debug::show($obj_main,$obj_sysmods,$obj_types);
			$db = factory::getDBO(); 
			$db->begin();
			//主表
		  $db->update('authgroup',$obj_main);
			
			//删除以前的明细表
			$db->query('delete from t_authgroup_sysmod where authgroup_id="'.$obj_main['id'].'" ');
		 
			//明细表 
			foreach($obj_sysmods as $v){ 
				$db->insert('authgroup_sysmod',array(
					'authgroup_id'=>$obj_main['id'],
					'sysmod_id'=>$v
				)); 
			} 
			$db->commit();
			exit('{success:1,msg:"修改权限组成功！"}');
		} 
		$this->assign('nav','权限组管理 - 修改权限组');

		
		 
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_authgroup` a  where a.id="'.$id.'" ');
		if(!$rs) url::msgBox('没有查到任何记录，请重新操作！','-1'); 
		$this->assign('rs',$rs);  
		$this->assign('rs_sysmod',$db->loadassoclist('select * from `t_authgroup_sysmod` where authgroup_id="'.$rs['id'].'" ','sysmod_id') );  
		
	 
		$sysmods = $db->loadassoclist('select a.*  from `t_sysmod` a where 0=0 order by  a.show_index '); 
 
		$this->assign('sysmods',$sysmods); 
		
	}
	/**
	 * 删除
	 */
	public function authgroup_del__(){
		//权限判断
		factory::module('admin')->authSys('authgroup');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$id = $_POST['id']; 
			$db = factory::getDBO();
			
			if($db->loadresult('select count(*) from t_user where deleted=0 and  authgroup_id='.$id)>0){
				exit('操作失败！已经有管理员使用此权限组，禁止删除！');
			}
			
			$sql = "delete from t_authgroup where id=".$id ;
			$delete = $db->query($sql);
			if($delete){
		    $db->query("delete from t_authgroup_sysmod where authgroup_id=".$id );
				exit('{success:1,msg:"操作成功！"}');
			}else{
				exit('系统繁忙，请稍后再试！');
			}
		}	
	}	
	# 预览
	public function authgroup_view__(){
		//权限判断
		factory::module('admin')->authSys('authgroup');
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_authgroup` a  where a.id="'.$id.'" ');
		if(!$rs) exit('没有查到任何记录，请重新操作！'); 
		$this->assign('rs',$rs);  
		$this->assign('rs_sysmod',$db->loadassoclist('select * from `t_authgroup_sysmod` where authgroup_id="'.$rs['id'].'" ','sysmod_id') );   
	 
		$sysmods = $db->loadassoclist('select a.*  from `t_sysmod` a where 0=0 order by  a.show_index '); 
	
		$this->assign('sysmods',$sysmods); 
		
	}
	
	##################################
	/* 后台用户管理 */
	##################################
	function users__(){
	
		//权限判断
		factory::module('admin')->authSys('adminuser');
	
		$this->assign('nav','后台用户管理'); 
		$this->assign('menuid1','1');  
		$this->assign('menuid2','2'); 
		$this->assign('myid',factory::module('admin')->getuserid());  
	}
	function users_list__(){
		//权限判断
		factory::module('admin')->authSys('adminuser');
		
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';  
  	  #查询条件
			$sql_where = ' where a.usertype = 1  and a.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.username' 	,'b.name' ));  
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_user` a  left join t_authgroup b on a.authgroup_id=b.id
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select 
					a.*,
					b.name as authgroup_name
				from `t_user` a  left join t_authgroup b on a.authgroup_id=b.id
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}  
	} 
	# 添加 
	public function users_add__(){ 
		//权限判断
		factory::module('admin')->authSys('adminuser');
		
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
		 
			$db = factory::getDBO(); 
			$db->begin();
			# 重名判断
			if($db->loadresult('select count(*) from t_user where deleted=0 and usertype in (0,1) and username="'.$form['username'].'" ')>0){
			
				exit('操作失败！【登录名】已经存在，请使用其他登录名！');
			}		
			//主表
			$id = $db->insert('user',array(
				'username'=>$form['username'],
				'password'=>$form['password'],
				'name'=>$form['name'],
				'authgroup_id'=>$form['authgroup_id'],
				'state'=>$form['state'] ,
				'usertype'=>'1'   ,
				'create_date'=>'now()',
				'create_ip'=>func::get_client_ip() 
			));
			if(!$id){
				$db->rollback();
				exit('系统繁忙，请稍后重试！');
			} 
			$db->commit();
			exit('{success:1,msg:"添加成功！"}');
		} 
		$this->assign('nav','后台用户管理 - 添加管理员');
		$db=factory::getDBO(); 
		$rs_authgroup = $db->loadassoclist('select a.*  from `t_authgroup` a where 0=0 order by  a.show_index ');  
		if(count($rs_authgroup) == 0){
			 url::msgBox('请先到【权限组管理】中添加权限组，再进行此操作！','-1'); 
		}
		$this->assign('rs_authgroup',$rs_authgroup); 
	}
	# 修改 
	public function users_modify__(){ 
		//权限判断
		factory::module('admin')->authSys('adminuser');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) )); 
 
			$db = factory::getDBO(); 
			$db->begin();
			//主表
		  $db->update('user',array(
				'id'=>$form['id'],
				'name'=>$form['name'],
				'state'=>$form['state'],
				'authgroup_id'=>$form['authgroup_id'] 
			));
			if($form['password'] != ''){
				$db->update('user',array(
					'id'=>$form['id'],
					'password'=>$form['password']
				));	
			}
			$db->commit();
			exit('{success:1,msg:"修改成功！"}');
		} 
		$this->assign('nav','后台用户管理 - 修改管理员信息');  
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_user` a  where deleted=0 and usertype=1 and a.id="'.$id.'" ');
		if(!$rs) url::msgBox('没有查到任何记录，请重新操作！','-1'); 
		$this->assign('rs',$rs);
		
		$rs_authgroup = $db->loadassoclist('select a.*  from `t_authgroup` a where 0=0 order by  a.show_index ');  
		$this->assign('rs_authgroup',$rs_authgroup); 
		
	}
	/**
	 * 删除
	 */
	public function users_del__(){
		//权限判断
		factory::module('admin')->authSys('adminuser');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$id = $_POST['id']; 
			$db = factory::getDBO(); 
			$sql = "update t_user set deleted=1 where usertype=1 and id=".$id ;
			$delete = $db->query($sql);
			if($delete){ 
				exit('{success:1,msg:"操作成功！"}');
			}else{
				exit('系统繁忙，请稍后再试！');
			}
		}	
	}	
	
	
 
	
	 
}
?>